const http = require('http')
const fs = require('fs')

let server = http.createServer((req, res) => { 
    /*
        解决跨域的几种方式，后端跨域一般只需要在请求头上面设置一下响应即可
    */ 
    res.setHeader("Access-Control-Allow-Origin","*")
    res.writeHead(200,{"Content-type" : "text/plain;charset=utf-8"})
    if (req.method === 'GET') {
        // console.log(req.body)
        if (req.url.split('?')[0] === '/name') {
            let obj = {
                name: 'jimmy',
                age: 22
            }
            setTimeout(() => {
                res.end(JSON.stringify(obj))
            }, 1000)
        }
    } else if (req.method === 'POST') { 
        let str = ''
        req.on('data', data => { 
            str += data
        })
        req.on('end', () => { 
            console.log('5555')
            res.end(str)
        })
    }
})



server.listen('8081', () => { 
    console.log('Server is running on port 8080')
})